package com.xiaoyan.texteditor.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * ClassName: FontDialog
 * Package: com.xiaoyan.texteditor.UI
 * Description:
 *  字体设置 对话框
 * @Author Xiaoyan_Lu
 * @Create 2023/12/5 17:33
 * @Version 1.0
 */
public class FontDialog extends JDialog {
    private static final int WIDTH = 425;
    private static final int HEIGHT = 300;
    private JComboBox<String> fontBox = new JComboBox<>();
    private JComboBox<String> fontType = new JComboBox<>();
    private JComboBox<String> fontSize = new JComboBox<>();
    //设置样例
    private JLabel example = new JLabel("AaBbCcDc",JLabel.CENTER);

    //当前字体
    private String chooseFontBody ="微软雅黑";//默认字体
    //当前字形
    private int chooseFontType = Font.PLAIN;//默认字形
    //当前大小
    private int chooseFontSize = 18;//默认大小

    //设置文本框字体
    private JTextArea jTextArea = null;
    public FontDialog(Frame owner, JTextArea textArea) {
        super(owner, "字体");
        this.setSize(WIDTH,HEIGHT);
        this.setLayout(new FlowLayout());
        //设置当前对话框的位置
        this.setFontDialogLocation(owner);
        this.jTextArea = textArea;
        //设置字体
        this.setFont(textArea.getFont());
        //设置字体
        this.setFontFunc();
        //设置字形
        this.setFontType();
        //设置大小
        this.setFontSize();
        //设置样例
        this.setExample();
        //设置确认选项
        this.setConfirmButton();
    }

    private void setFontDialogLocation(Frame owner) {
        int x = owner.getX() + owner.getWidth()/2 - this.getWidth()/2;
        int y = owner.getY()+ owner.getHeight()/2 - this.getHeight()/2;
        this.setLocation(x,y);
    }

    @Override
    public void setFont(Font f) {
        chooseFontBody = f.getFamily();
        chooseFontType = f.getStyle();
        chooseFontSize = f.getSize();
    }

    /* *
     * @Description: 设置确认选项
     * @param []
     * @return void
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 16:54
     */
    private void setConfirmButton() {
        JButton yesJButton = new JButton("确定");
        JButton noJButton = new JButton("取消");
        this.getContentPane().add(yesJButton);
        this.getContentPane().add(noJButton);
        //注册监听器
        yesJButton.addActionListener(new YesHandler());
        noJButton.addActionListener(new NoHandler());
    }

    /* *
     * @Description: 设置显示样例
     * @param []
     * @return void
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 16:55
     */
    private void setExample() {
        example.setPreferredSize(new Dimension(400,180));
        example.setBorder(BorderFactory.createTitledBorder("样例"));
        example.setFont(new Font(chooseFontBody,chooseFontType,chooseFontSize));
        this.getContentPane().add(example,BorderLayout.CENTER);
    }

    /* *
     * @Description: 设置字体大小
     * @param []
     * @return void
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 16:55
     */
    private void setFontSize() {
        this.getContentPane().add(new JLabel("大小"));
        for(int i = 8; i <= 48; i++){
            fontSize.addItem(i+"");
        }
        fontSize.addItem("72");
        this.getContentPane().add(fontSize);
        fontSize.setSelectedItem(String.valueOf(chooseFontSize));
        //给大小注册监听器
        fontSize.addActionListener(new FontSizeHandler());
    }

    /* *
     * @Description: 设置字形
     * @param []
     * @return void
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 16:55
     */
    private void setFontType() {
        this.getContentPane().add(new JLabel("字形:"));
        fontType.addItem("常规");
        fontType.addItem("粗体");
        fontType.addItem("斜体");
        String choose = "";
        switch (chooseFontType){
            case 0:
                choose = "常规";
                break;
            case 1:
                choose = "粗体";
                break;
            case 2:
                choose = "斜体";
                break;
            default:
                System.out.println("Error");
        }
        fontType.setSelectedItem(choose);
        this.getContentPane().add(fontType);
        //注册监听器
        fontType.addActionListener(new FontTypeHandler());
    }

    /* *
     * @Description: 设置字体
     * @param []
     * @return void
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 15:22
     */
    private void setFontFunc() {
        this.getContentPane().add(new JLabel("字体:"));
        fontBox.addItem("楷体");
        fontBox.addItem("宋体");
        fontBox.addItem("微软雅黑");
        this.getContentPane().add(fontBox);
        fontBox.setSelectedItem(chooseFontBody);
        //注册监听器
        fontBox.addActionListener(new FontHandler());
    }

    private class FontHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            chooseFontBody = fontBox.getSelectedItem().toString();
            //更新样例显示
            updateShowExample();
        }
    }

    /* *
     * @Description: 跟新显示样例
     * @param []
     * @return void
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 16:24
     */
    private void updateShowExample() {
        this.example.setFont(new Font(chooseFontBody,chooseFontType,chooseFontSize));
    }

    private class FontTypeHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            switch (fontType.getSelectedItem().toString()){
                case "常规":
                    chooseFontType = Font.PLAIN;
                    break;
                case "粗体":
                    chooseFontType = Font.BOLD;
                    break;
                case "斜体":
                    chooseFontType = Font.ITALIC;
                    break;
                default:
                    System.out.println("Error");
            }
            updateShowExample();
        }
    }

    /* *
     * @Description: 调整字体大小
     * @param
     * @return 
     * @version v1.0
     * @author xiaoyan_lu
     * @date 2023/12/6 16:48
     */
    private class FontSizeHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            chooseFontSize = Integer.valueOf(fontSize.getSelectedItem().toString());
            updateShowExample();
        }
    }

    private class YesHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            jTextArea.setFont(new Font(chooseFontBody,chooseFontType,chooseFontSize));
            dispose();
        }
    }

    private class NoHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    }
}
